package com.example.term;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class login extends AppCompatActivity implements View.OnClickListener {
    //Fragment
    //Fragment
    private Fragment Fragment_chat = new chat();
    private Fragment Fragment_weather= new weather();
    private Fragment Fragment_calculate = new calculate();

    //底端菜单栏LinearLayout
    private LinearLayout linear_chat;
    private LinearLayout linear_weather;
    private LinearLayout linear_calculate;
    private LinearLayout linear_bootom;


    //底端菜单栏中的Imageview
    private ImageView imageView_chat;
    private ImageView imageView_weather;
    private ImageView imageView_calculate;

    //FragmentManager
    private FragmentManager fragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        linear_chat=findViewById(R.id.linear1);
        linear_weather=findViewById(R.id.linear3);
        linear_calculate=findViewById(R.id.linear2);
        linear_bootom = findViewById(R.id.linear_bootom);

        linear_chat.setOnClickListener(this);
        linear_weather.setOnClickListener(this);
        linear_calculate.setOnClickListener(this);
        initFragment();
        showfragmnet(0);

    }
    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.linear1:
                showfragmnet(0);
                break;
            case R.id.linear2:
                showfragmnet(1);
                break;
            case R.id.linear3:
                showfragmnet(2);
                break;

            default:

                break;
        }
    }

    private void initFragment(){
        fragmentManager=getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction(); //开启一个事务
        transaction.add(R.id.id_content,Fragment_chat);             //添加一个和Fragment
        transaction.add(R.id.id_content,Fragment_weather);
        transaction.add(R.id.id_content,Fragment_calculate);

        transaction.commit();       // 提交事务
    }
    private void hideFragment(FragmentTransaction transaction){  // 隐藏所有的Fragment
        transaction.hide(Fragment_chat);
        transaction.hide(Fragment_calculate);
        transaction.hide(Fragment_weather);

    }
    private void background(View v) {  //设置被选中的颜色

        switch (v.getId()) {
            case R.id.linear1:
                linear_bootom.setBackgroundColor(Color.parseColor("#FF39A2D1"));
                linear_chat.setBackgroundColor(Color.parseColor("#FF39A2D1"));

                break;
            case R.id.linear2:
                linear_bootom.setBackgroundColor(Color.parseColor("#FF000000"));
                linear_calculate.setBackgroundColor(Color.parseColor("#FF000000"));
                break;
            case R.id.linear3:
                linear_bootom.setBackgroundColor(Color.parseColor("#FF000000"));
                linear_weather.setBackgroundColor(Color.parseColor("#FF000000"));
                break;

            default:
                break;
        }
    }

    private void backgroundreturn(View v) {  //设置未被选中的颜色
        switch (v.getId()) {
            case R.id.linear1:
                linear_chat.setBackgroundColor(Color.parseColor("#2196F3"));
                break;
            case R.id.linear2:
                linear_calculate.setBackgroundColor(Color.parseColor("#2196F3"));
                break;
            case R.id.linear3:
                linear_weather.setBackgroundColor(Color.parseColor("#2196F3"));
                break;

            default:
                break;
        }
    }

    private void showfragmnet(int i) {  //显示Fragment

        FragmentTransaction transaction=fragmentManager.beginTransaction();
        hideFragment(transaction);  // 隐藏所有的碎片 为显示下一个碎片做准备
        switch (i){
            case 0:
                transaction.show(Fragment_chat);
                background(linear_chat);
                backgroundreturn(linear_calculate);
                backgroundreturn(linear_weather);

                break;
            case 1:
                transaction.show(Fragment_calculate);
                background(linear_calculate);
                backgroundreturn(linear_weather);
                backgroundreturn(linear_chat);

                break;
            case 2:
                transaction.show(Fragment_weather);
                background(linear_weather);
                backgroundreturn(linear_chat);
                backgroundreturn(linear_calculate);

                break;

            default:
                break;
        }
        transaction.commit();
    }




}